<?php $this->load->view("partial/header"); ?>
<style type="text/css">
    #finish_sale_button span {
        background: url("<?php echo base_url(); ?>/images/pieces/complete_sale.png") no-repeat scroll -10px -10px transparent;
        color: #FFFFFF;
        cursor: pointer;
        display: block;
        height: 25px;
        padding-top: 5px;
        text-align: center;
        width: 220px;
        margin-top:10px;
    }
</style>
<?php
if (isset($error_message)) {
    echo '<h1 style="text-align: center;">' . $error_message . '</h1>';
    exit;
}
?>
<div id="receipt_wrapper">
    <table class="tablelist">
<!--	<tr>		
            <td <?php if ($this->config->item('gst')) { ?>colspan="5"<?php } else { ?>colspan="5"<?php } ?>>
        <?php if ($this->config->item('company_logo')) { ?>
                                                            <div id="company_logo_sale"><?php echo img(array('src' => $this->Appconfig->get_logo_image(), 'width' => 50, 'height' => 50)); ?></div>
        <?php } ?>
                    <div id="company_name_sale"><?php echo $this->config->item('company'); ?></div>
            </td>
    </tr>-->
        <tr>
            <td <?php if ($this->config->item('gst')) { ?>colspan="5"<?php } else { ?>colspan="5"<?php } ?> style="text-align:center;font-size:12px;">
                <?php echo lang('common_abn') . ": " . $this->config->item('abn'); ?>
            </td>
        </tr>
        <tr>
            <td <?php if ($this->config->item('gst')) { ?>colspan="5"<?php } else { ?>colspan="5"<?php } ?> style="text-align:center;font-size:12px;">
                <?php echo $receipt_title; ?>
            </td>
        </tr>
        <tr>
            <td <?php if ($this->config->item('gst')) { ?>colspan="5"<?php } else { ?>colspan="5"<?php } ?> style='text-align:right;font-size:10px;'>
                <div id="sale_id"><?php echo lang('sales_no') . ': '; ?><?php echo(isset($layby_sale_id) && $layby_sale_id > 0 ? $layby_sale_id : $sale_id); ?></div>

                <div id="company_address_sale"><?php echo nl2br($this->config->item('address')); ?></div>
                <div id="company_phone_sale"><?php echo lang('common_phone_number') . ": " . $this->config->item('phone'); ?></div>
            </td>
        </tr>
        <?php
        if (isset($customer)) {
            ?>
            <tr>
                <td <?php if ($this->config->item('gst')) { ?>colspan="5"<?php } else { ?>colspan="5"<?php } ?>>
                    <div id="receipt_general_info">
                        <div id="customer_name_sale"><?php echo lang('customers_customer') . ": " . $customer; ?></div>
                        <?php if ($customer_address != "") { ?>
                            <div id="customer_address"><?php echo lang('common_address_1') . ": " . $customer_address . "," . $customer_suburd . "," . $customer_state . "," . $customer_postcode; ?></div>
                        <?php } ?>
                        <?php if ($customer_phone != "") { ?>
                            <div id="customer_phone"><?php echo lang('common_phone_number') . ": " . $customer_phone; ?></div>
                        <?php } ?>		
                    </div>		
                </td>
            </tr>
            <?php
        }
        ?>		
        <tr>
            <td <?php if ($this->config->item('gst')) { ?>colspan="5"<?php } else { ?>colspan="5"<?php } ?>>
                <div id="employee_sale"><?php echo lang('employees_staff') . ": " . $employee; ?></div>
                <div id="sale_time"><?php echo lang('employees_date') . ": " . $transaction_time ?></div>			
            </td>		
        </tr>
        <tr>
            <td style="<?php if ($this->config->item('gst')) { ?>width:635px;<?php } else { ?>width:655px;<?php } ?>font-size:10px;"><?php echo lang('items_item'); ?></td>
            <td style="300px;font-size:10px;">Barcode</td>
            <td style="width:20px;font-size:10px;"><?php echo lang('common_price'); ?></td>
            <td style="width:5px;text-align:center;font-size:10px;"><?php echo lang('sales_quantity'); ?></td>
            <?php if ($this->config->item('gst')) { ?>
                <td style="width:20px;font-size:10px;"><?php echo lang('config_gst'); ?></td>
            <?php } ?>
            <td style="width:20px;text-align:right;font-size:10px;"><?php echo lang('sales_sub_total'); ?></td>
        </tr>
        <?php
        foreach (array_reverse($cart, true) as $line => $item) {
            ?>
            <tr>
                <td>
                    <div id="receipt_general_info">
                        <div id="item_name"><span class='long_name'><?php echo $item['name']; ?></span><span class='short_name'><?php echo $item['name']; ?></span></div>
                        <?php if ($item['serialnumber'] != "") { ?>
                            <div id="item_serial"><?php echo lang('items_serial') . ": " . $item['serialnumber']; ?></div>
                        <?php } ?>
                        <?php if ($item['description'] != "") { ?>
                            <div id="item_description"><?php echo $item['description']; ?></div>
                        <?php } ?>			
                    </div>
                </td>
                <td style='font-size:10px;'><?php echo $item['item_number']; ?></td>
                <?php
                $price = 0;
                if ($item['discount'] < 0) {
                    $price+=($item['price'] - abs($item['discount']));
                } else {
                    $price+=($item['price'] + abs($item['discount']));
                }
                ?>
                <td><div id="sale_price"><?php echo to_currency($price); ?></div></td>
                <td style='text-align:center;font-size:10px'><div id="sale_qty"><?php echo $item['quantity']; ?></div></td>
                <?php if ($this->config->item('gst')) { ?>
                    <td style='text-align:center;font-size:10px'><div id="sale_gst_price"><?php echo to_currency($price / 11); ?></div></td>
                <?php } ?>	
                <?php
                $subtotal = 0;
                if ($item['discount'] < 0) {
                    $subtotal+=($item['price'] * $item['quantity'] - abs($item['discount']));
                } else {
                    $subtotal+=($item['price'] * $item['quantity'] + abs($item['discount']));
                }
                ?>
                <td style='text-align:right;font-size:10px'><div id="sale_totle"><?php if ($this->config->item('gst')) { ?><?php echo to_currency($subtotal + $subtotal / 11); ?><?php } else { ?><?php echo to_currency($subtotal); ?><?php } ?></div></td>
            </tr>

            <?php
        }
        ?>
        <?php if ($this->config->item('gst')) { ?>
            <tr>
                <td <?php if ($this->config->item('gst')) { ?>colspan="3"<?php } else { ?>colspan="3"<?php } ?> style='text-align:right;font-size:10px'><?php echo lang('config_gst'); ?></td>
                <td colspan="2" style='text-align:right;font-weight: bold;font-size:10px'><div id="sale_totle"><?php echo to_currency($total / 11); ?></div></td>
            </tr>	
        <?php } ?>
        <tr>
            <td <?php if ($this->config->item('gst')) { ?>colspan="3"<?php } else { ?>colspan="3"<?php } ?> style='text-align:right;font-size:10px'><?php if ($this->config->item('gst')) { ?><?php echo lang('sales_total_gst'); ?><?php } else { ?><?php echo lang('sales_total'); ?><?php } ?></td>
            <td colspan="2" style='text-align:right;font-weight: bold;font-size:10px'><div id="sale_totle"><?php if ($this->config->item('gst')) { ?><?php echo to_currency($total + $total / 11); ?><?php } else { ?><?php echo to_currency($total); ?><?php } ?></div></td>
        </tr>
        <tr>
            <td colspan="3" style='text-align:right;font-size:10px'><?php echo lang('layby_amount_due'); ?></td>
            <td colspan="2" style='text-align:right;font-weight: bold;font-size:10px'><div id="sale_totle"><?php echo to_currency($balance); ?></div></td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;</td>
            <td colspan="2" style="text-align:right;font-weight: bold;font-size:10px"><?php echo lang('layby_paid'); ?></td>
            <td style="text-align:right;font-weight: bold;font-size:10px"><?php echo lang('layby_pickup'); ?></td>
        </tr>
        <?php
        for ($index = 0; $index < count($deposits); $index++) {
            $times = $index + 1;
            $deposit = $deposits[$index];
            $text = lang('layby_deposit') . ' - ' . $times;
            $pickup = date('d-m-Y', strtotime($deposit->pickup));
            ?>
            <tr>
                <td <?php if ($this->config->item('gst')) { ?>colspan="2"<?php } else { ?>colspan="2"<?php } ?> style='text-align:right;font-size:10px'><?php if ($this->config->item('gst')) { ?><?php echo $text; ?><?php } else { ?><?php echo $text; ?><?php } ?></td>
                <td colspan="2" style='text-align:right;font-weight: bold;font-size:10px'><div id="deposit_list"><?php if ($this->config->item('gst')) { ?><?php echo to_currency($deposit->deposit); ?><?php } else { ?><?php echo to_currency($deposit->deposit); ?><?php } ?></div></td>
                <td colspan="2" style='text-align:right;font-weight: bold;font-size:10px'><div id=pickup_list"><?php if ($this->config->item('gst')) { ?><?php echo $pickup; ?><?php } else { ?><?php echo $pickup; ?><?php } ?></div></td>
            </tr>
            <?php
        }
        ?>
        <tr>
            <td <?php if ($this->config->item('gst')) { ?>colspan="5"<?php } else { ?>colspan="5"<?php } ?>>
                <div id="sale_totle"><i><?php echo lang('items_term'); ?></i></div>
            </td>
        </tr>
        <tr>
            <td <?php if ($this->config->item('gst')) { ?>colspan="5"<?php } else { ?>colspan="5"<?php } ?> style='text-align:left;'>
                <div id="sale_return_policy">
                    <?php echo nl2br($this->config->item('return_policy')); ?>
                </div>		
            </td>
        </tr>
        <tr>
            <td colspan="5" style="border:none">
                <div id="finish_sale_button" class="small_button">
                    <span><?php echo $layby_print; ?></span> 
                </div>
            </td>
        </tr>
    </table>
</div>
<?php $this->load->view("partial/footer"); ?>

<?php
if ($this->Appconfig->get('print_after_sale')) {
    ?>
    <script type="text/javascript">
        $(window).load(function()
        {
            window.print();
                                        
            $('#finish_sale_button').click(function(){
                window.print();
                return false;
            });
        });
    </script>
    <?php
}
?>